package com.yifi.system.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.SysUtil;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.StringUtils;
import com.yifi.system.domain.SysTreeModel;

import cn.hutool.core.util.StrUtil;

public class SysDeptService {

    /**
     * 对象转菜单树
     *
     * @param cmsCategoryList 菜单列表
     * @return 树结构列表
     */
    public List<SysTreeModel> initZtree(List<Record> cmsCategoryList)
    {
        return initZtree(cmsCategoryList, null, false);
    }

    /**
     * 对象转菜单树
     *
     * @param cmsCategoryList 菜单列表
     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag 是否需要显示权限标识
     * @return 树结构列表
     */
    public List<SysTreeModel> initZtree(List<Record> list, List<String> roleMenuList, boolean permsFlag)
    {
        List<SysTreeModel> ztrees = new ArrayList<SysTreeModel>();
        boolean isCheck = StringUtils.isNotNull(roleMenuList);
        for (Record record : list)
        {
        	SysTreeModel CategoryTree = new SysTreeModel();
            CategoryTree.setId(record.get("ccode"));
            CategoryTree.setPid(record.get("pcode"));
            CategoryTree.setName(transMenuName(record, permsFlag));
            CategoryTree.setTitle(record.getStr("cname"));
            if (isCheck)
            {
                CategoryTree.setChecked(roleMenuList.contains(record.getStr("ccode")));
            }
            ztrees.add(CategoryTree);
        }
        return ztrees;
    }


    public String transMenuName(Record record, boolean permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(record.getStr("cname"));
        /*if (permsFlag)
        {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + cmsCategory.getPerms() + "</font>");
        }*/
        return sb.toString();
    }
    
    @Inject
    private MisUserService misUserService;
    
    public boolean addOrg(String c_pro,String cname,String cphone
    		,String cemail,String user_num,String cpass)
    {
    	String sql="select * from mis_org where c_pro='"+c_pro+"' and ilayer='1' ";
    	Record record=Db.findFirst(sql);
    	String mcode="";
    	boolean isedit=true;
    	if(record==null)
    	{
        	sql="select max(ccode) from mis_org where ilayer='1' ";
        	mcode=Db.queryStr(sql);
        	if(StrUtil.isEmpty(mcode))
        	{
        		mcode="01";
        	}
        	else
        	{
        		mcode=SysUtil.getFillNum((Integer.parseInt(mcode)+1)+"",2);
        	}
        	isedit=false;
        	record=new Record();
    		record.set("ccode", mcode);
    		record.set("ilayer", "1");
    		record.set("ctype", "0");
    		record.set("cgroup", "0");
    		record.set("isort", "1");
    		record.set("c_pro",c_pro);
    		record.set("pcode","0");
    	}
    	
    	record.set("cname", cname);
    	if(DbUtils.save("mis_org", "ccode", record, isedit))
    	{
    		if(!isedit)
    		{
    			misUserService.addUser(c_pro, mcode, user_num, cname, cphone, cemail, cpass);
    		}
    	}
    	
    	return true;
    	
    }
}
